package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Estudiante;
import com.lostreseditores.sabersai.model.Genero;
import com.lostreseditores.sabersai.model.dto.EstudianteDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class EstudianteView {
    private InputText txtEstucodigov;
    private InputText txtEstudocumv;
    private InputText txtEstuemailv;
    private InputText txtEstunombrev;
    private InputText txtGeneidn_Genero;
    private InputText txtEstuidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<EstudianteDTO> data;
    private EstudianteDTO selectedEstudiante;
    private SelectOneMenu cmbSexo;
    private Integer sexoSelect;
    private List<Genero> listSexo = new ArrayList<Genero>();
    private SelectOneMenu cmbSexo2;
    private Integer sexoSelect2;
    private List<Genero> listSexo2 = new ArrayList<Genero>();


    public EstudianteView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            EstudianteDTO estudianteDTO = (EstudianteDTO) e.getObject();

            if (txtEstucodigov == null) {
                txtEstucodigov = new InputText();
            }

            txtEstucodigov.setValue(estudianteDTO.getEstucodigov());

            if (txtEstudocumv == null) {
                txtEstudocumv = new InputText();
            }

            txtEstudocumv.setValue(estudianteDTO.getEstudocumv());

            if (txtEstuemailv == null) {
                txtEstuemailv = new InputText();
            }

            txtEstuemailv.setValue(estudianteDTO.getEstuemailv());

            if (txtEstunombrev == null) {
                txtEstunombrev = new InputText();
            }

            txtEstunombrev.setValue(estudianteDTO.getEstunombrev());

            if (txtGeneidn_Genero == null) {
                txtGeneidn_Genero = new InputText();
            }

            txtGeneidn_Genero.setValue(estudianteDTO.getGeneidn_Genero());

            if (txtEstuidn == null) {
                txtEstuidn = new InputText();
            }

            txtEstuidn.setValue(estudianteDTO.getEstuidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtEstucodigov != null) {
            txtEstucodigov.setValue(null);
            txtEstucodigov.setDisabled(true);
        }

        if (txtEstudocumv != null) {
            txtEstudocumv.setValue(null);
            txtEstudocumv.setDisabled(true);
        }

        if (txtEstuemailv != null) {
            txtEstuemailv.setValue(null);
            txtEstuemailv.setDisabled(true);
        }

        if (txtEstunombrev != null) {
            txtEstunombrev.setValue(null);
            txtEstunombrev.setDisabled(true);
        }

        if (txtGeneidn_Genero != null) {
            txtGeneidn_Genero.setValue(null);
            txtGeneidn_Genero.setDisabled(true);
        }

        if (txtEstuidn != null) {
            txtEstuidn.setValue(null);
            txtEstuidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Estudiante entity = null;

        try {
            Integer estuidn = new Integer(txtEstuidn.getValue().toString());
            entity = BusinessDelegatorView.getEstudiante(estuidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtEstucodigov.setDisabled(false);
            txtEstudocumv.setDisabled(false);
            txtEstuemailv.setDisabled(false);
            txtEstunombrev.setDisabled(false);
            txtGeneidn_Genero.setDisabled(false);
            txtEstuidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtEstucodigov.setValue(entity.getEstucodigov());
            txtEstucodigov.setDisabled(false);
            txtEstudocumv.setValue(entity.getEstudocumv());
            txtEstudocumv.setDisabled(false);
            txtEstuemailv.setValue(entity.getEstuemailv());
            txtEstuemailv.setDisabled(false);
            txtEstunombrev.setValue(entity.getEstunombrev());
            txtEstunombrev.setDisabled(false);
            txtGeneidn_Genero.setValue(entity.getGenero().getGeneidn());
            txtGeneidn_Genero.setDisabled(false);
            txtEstuidn.setValue(entity.getEstuidn());
            txtEstuidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

/*    public String action_save() {
        try {
            BusinessDelegatorView.saveEstudiante(FacesUtils.checkString(
                    txtEstucodigov), FacesUtils.checkString(txtEstudocumv),
                FacesUtils.checkString(txtEstuemailv),
                FacesUtils.checkInteger(txtEstuidn),
                FacesUtils.checkString(txtEstunombrev),
                FacesUtils.checkInteger(txtGeneidn_Genero));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }
    */
	
    public String action_delete() {
        try {
            BusinessDelegatorView.deleteEstudiante(FacesUtils.checkInteger(
                    txtEstuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateEstudiante(FacesUtils.checkString(
                    txtEstucodigov), FacesUtils.checkString(txtEstudocumv),
                FacesUtils.checkString(txtEstuemailv),
                FacesUtils.checkInteger(txtEstuidn),
                FacesUtils.checkString(txtEstunombrev),
                FacesUtils.checkInteger(txtGeneidn_Genero));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtEstuidn == null) {
                txtEstuidn = new InputText();
            }

            txtEstuidn.setValue(selectedEstudiante.getEstuidn());

            BusinessDelegatorView.deleteEstudiante(FacesUtils.checkInteger(
                    txtEstuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedEstudiante);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String estucodigov, String estudocumv,
        String estuemailv, Integer estuidn, String estunombrev,
        Integer geneidn_Genero) throws Exception {
        try {
            BusinessDelegatorView.updateEstudiante(estucodigov, estudocumv,
                estuemailv, estuidn, estunombrev, geneidn_Genero);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("EstudianteView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtEstucodigov() {
        return txtEstucodigov;
    }

    public void setTxtEstucodigov(InputText txtEstucodigov) {
        this.txtEstucodigov = txtEstucodigov;
    }

    public InputText getTxtEstudocumv() {
        return txtEstudocumv;
    }

    public void setTxtEstudocumv(InputText txtEstudocumv) {
        this.txtEstudocumv = txtEstudocumv;
    }

    public InputText getTxtEstuemailv() {
        return txtEstuemailv;
    }

    public void setTxtEstuemailv(InputText txtEstuemailv) {
        this.txtEstuemailv = txtEstuemailv;
    }

    public InputText getTxtEstunombrev() {
        return txtEstunombrev;
    }

    public void setTxtEstunombrev(InputText txtEstunombrev) {
        this.txtEstunombrev = txtEstunombrev;
    }

    public InputText getTxtGeneidn_Genero() {
        return txtGeneidn_Genero;
    }

    public void setTxtGeneidn_Genero(InputText txtGeneidn_Genero) {
        this.txtGeneidn_Genero = txtGeneidn_Genero;
    }

    public InputText getTxtEstuidn() {
        return txtEstuidn;
    }

    public void setTxtEstuidn(InputText txtEstuidn) {
        this.txtEstuidn = txtEstuidn;
    }

    public List<EstudianteDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataEstudiante();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<EstudianteDTO> estudianteDTO) {
        this.data = estudianteDTO;
    }

    public EstudianteDTO getSelectedEstudiante() {
        return selectedEstudiante;
    }

    public void setSelectedEstudiante(EstudianteDTO estudiante) {
        this.selectedEstudiante = estudiante;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

	public SelectOneMenu getCmbSexo() {
		return cmbSexo;
	}

	public void setCmbSexo(SelectOneMenu cmbSexo) {
		this.cmbSexo = cmbSexo;
	}

	public Integer getSexoSelect() {
		return sexoSelect;
	}

	public void setSexoSelect(Integer sexoSelect) {
		this.sexoSelect = sexoSelect;
	}

	public List<Genero> getListSexo() {
		listSexo.clear();

		try {
			listSexo = BusinessDelegatorView.getGenero();

		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return listSexo;
	}

	public void setListSexo(List<Genero> listSexo) {
		this.listSexo = listSexo;
	}
	
	
	
    public SelectOneMenu getCmbSexo2() {
		return cmbSexo2;
	}

	public void setCmbSexo2(SelectOneMenu cmbSexo2) {
		this.cmbSexo2 = cmbSexo2;
	}

	public Integer getSexoSelect2() {
		return sexoSelect2;
	}

	public void setSexoSelect2(Integer sexoSelect2) {
		this.sexoSelect2 = sexoSelect2;
	}

	public List<Genero> getListSexo2() {
		listSexo2.clear();

		try {
			listSexo2 = BusinessDelegatorView.getGenero();

		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return listSexo2;
	}

	public void setListSexo2(List<Genero> listSexo2) {
		this.listSexo2 = listSexo2;
	}


    
    
}
